using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The EducationPeriodTableDB class is responsible for interacting with the database to retrieve and store information
    /// about EducationPeriodTable objects.
    /// </summary>
    public class EducationPeriodTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of EducationPeriodTable from the underlying datasource.
        /// </summary>
        /// <param name="educationPeriodKey">The unique EducationPeriodKey of the EducationPeriodTable in the database.</param>
        /// <returns>An EducationPeriodTable when the EducationPeriodKey was found in the database, or null otherwise.</returns>
        public static EducationPeriodTable GetItem(int educationPeriodKey)
        {
            EducationPeriodTable myEducationPeriodTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@educationPeriodKey", educationPeriodKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myEducationPeriodTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myEducationPeriodTable;
            }
        }

        /// <summary>
        /// Returns a list with EducationPeriodTable objects.
        /// </summary>
        /// <returns>A generics List with the EducationPeriodTable objects.</returns>
        public static EducationPeriodTableList GetList()
        {
            EducationPeriodTableList tempList = new EducationPeriodTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }


        /// <summary>
        /// Returns a list with EducationPeriodTable objects.
        /// </summary>
        /// <returns>A generics List with the EducationPeriodTable objects.</returns>
        public static EducationPeriodTableList GetList(int SchoolKey)
        {
            EducationPeriodTableList tempList = new EducationPeriodTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a EducationPeriodTable in the database.
        /// </summary>
        /// <param name="myEducationPeriodTable">The EducationPeriodTable instance to save.</param>
        /// <returns>The new EducationPeriodKey if the EducationPeriodTable is new in the database or the existing EducationPeriodKey when an item was updated.</returns>
        public static int Save(EducationPeriodTable myEducationPeriodTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myEducationPeriodTable.EducationPeriodKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", myEducationPeriodTable.EducationPeriodKey);
                    }

                    if (myEducationPeriodTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myEducationPeriodTable.SchoolKey);
                    }

                    if (myEducationPeriodTable.LocationKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", myEducationPeriodTable.LocationKey);
                    }

                    if (myEducationPeriodTable.StandardLevelKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", myEducationPeriodTable.StandardLevelKey);
                    }


                    if (string.IsNullOrEmpty(myEducationPeriodTable.EducationPeriodCode))
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodCode", myEducationPeriodTable.EducationPeriodCode);
                    }

                    if (string.IsNullOrEmpty(myEducationPeriodTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myEducationPeriodTable.Description);
                    }

                    if (myEducationPeriodTable.StartDate <= DateTime.MinValue)
                    {
                        myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@startDate", myEducationPeriodTable.StartDate);
                    }
                    if (myEducationPeriodTable.EndDate <= DateTime.MinValue)
                    {
                        myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@endDate", myEducationPeriodTable.EndDate);
                    }

                    myCommand.Parameters.AddWithValue("@status", myEducationPeriodTable.Status);

                    //if (myEducationPeriodTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myEducationPeriodTable.CreatedDateTime);
                    //}
                    //if (myEducationPeriodTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myEducationPeriodTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a EducationPeriodTable from the database.
        /// </summary>
        /// <param name="educationPeriodKey">The EducationPeriodKey of the EducationPeriodTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int educationPeriodKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@educationPeriodKey", educationPeriodKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the EducationPeriodTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static EducationPeriodTable FillDataRecord(IDataRecord myDataRecord)
        {
            EducationPeriodTable myEducationPeriodTable = new EducationPeriodTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodKey")))
            {
                myEducationPeriodTable.EducationPeriodKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myEducationPeriodTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myEducationPeriodTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                myEducationPeriodTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodCode")))
            {
                myEducationPeriodTable.EducationPeriodCode = myDataRecord.GetString(myDataRecord.GetOrdinal("EducationPeriodCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myEducationPeriodTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
            {
                myEducationPeriodTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
            {
                myEducationPeriodTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myEducationPeriodTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myEducationPeriodTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myEducationPeriodTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myEducationPeriodTable;
        }
    }

}
